//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "InflationTermStructure.h"
using namespace Cephei::QL::Termstructures;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Inflation/Seasonality.h>
#include <gen/QL/TermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Inflation;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (boost::shared_ptr<QuantLib::InflationTermStructure>& childNative, Object^ owner) : CTermStructure(CInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phInflationTermStructure = NULL;
#endif
	_ppInflationTermStructure = &childNative;
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
}
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (QuantLib::InflationTermStructure& childNative, Object^ owner) : CTermStructure(CInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phInflationTermStructure = NULL;
#endif
	_ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (&childNative);
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
    _InflationTermStructureOwner = owner;
    _TermStructureOwner = owner;
}

Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (CInflationTermStructure^ copy) : CTermStructure(CInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phInflationTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (copy->GetShared());
        _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
    }
}
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (PLATFORM::Type^ t) : CTermStructure(CInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phInflationTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CInflationTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (QuantLib::Handle<QuantLib::InflationTermStructure>& childNative, Object^ owner)  : CTermStructure(CInflationTermStructure::typeid)
{
	_phInflationTermStructure = &childNative;
	_ppInflationTermStructure = &static_cast<boost::shared_ptr<QuantLib::InflationTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
    _InflationTermStructureOwner = owner;
}
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (QuantLib::Handle<QuantLib::InflationTermStructure> childNative)  : CTermStructure(CInflationTermStructure::typeid)
{
	_phInflationTermStructure = &childNative;
	_ppInflationTermStructure = &static_cast<boost::shared_ptr<QuantLib::InflationTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::CInflationTermStructure::CInflationTermStructure (QuantLib::InflationTermStructure childNative)  : CTermStructure(CInflationTermStructure::typeid)
{
#ifdef HANDLE
	_phInflationTermStructure = NULL;
#endif
	_ppInflationTermStructure = new boost::shared_ptr<QuantLib::InflationTermStructure> (new QuantLib::InflationTermStructure (childNative));
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppInflationTermStructure));
}
#endif

Cephei::QL::Termstructures::CInflationTermStructure::~CInflationTermStructure ()
{
    if (_ppInflationTermStructure != NULL)
    {
	    delete _ppInflationTermStructure;
        _ppInflationTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::CInflationTermStructure::!CInflationTermStructure ()
{
    if (_ppInflationTermStructure != NULL)
    {
	    delete _ppInflationTermStructure;
    }
}
QuantLib::InflationTermStructure& Cephei::QL::Termstructures::CInflationTermStructure::GetReference ()
{
    if (_ppInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppInflationTermStructure;
}
boost::shared_ptr<QuantLib::InflationTermStructure>& Cephei::QL::Termstructures::CInflationTermStructure::GetShared ()
{
    if (_ppInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppInflationTermStructure;
}
QuantLib::InflationTermStructure* Cephei::QL::Termstructures::CInflationTermStructure::GetPointer ()
{
    if (_ppInflationTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppInflationTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InflationTermStructure>& Cephei::QL::Termstructures::CInflationTermStructure::GetHandle ()
{
	if (_phInflationTermStructure == NULL)
	{
		_phInflationTermStructure = new Handle<QuantLib::InflationTermStructure> (*_ppInflationTermStructure);
	}
	return *_phInflationTermStructure;
}
#endif
bool Cephei::QL::Termstructures::CInflationTermStructure::HasNative () 
{
	return (_ppInflationTermStructure != NULL);
}

DateTime Cephei::QL::Termstructures::CInflationTermStructure::BaseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppInflationTermStructure)->baseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::CInflationTermStructure::BaseRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationTermStructure)->baseRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::FrequencyEnum Cephei::QL::Termstructures::CInflationTermStructure::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppInflationTermStructure)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Termstructures::CInflationTermStructure::NominalTermStructure::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppInflationTermStructure)->nominalTermStructure ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Termstructures::CInflationTermStructure::HasSeasonality::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppInflationTermStructure)->hasSeasonality ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Termstructures::CInflationTermStructure::IndexIsInterpolated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppInflationTermStructure)->indexIsInterpolated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::CInflationTermStructure::ObservationLag::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppInflationTermStructure)->observationLag ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Inflation::ISeasonality^ Cephei::QL::Termstructures::CInflationTermStructure::Seasonality::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Seasonality> _rv = (boost::shared_ptr<QuantLib::Seasonality>)(*_ppInflationTermStructure)->seasonality ( );   
        Cephei::QL::Termstructures::Inflation::CSeasonality^ _nrv = REFNEW Cephei::QL::Termstructures::Inflation::CSeasonality (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IInflationTermStructure^ Cephei::QL::Termstructures::CInflationTermStructure::SetSeasonality (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Inflation::ISeasonality^>^ seasonality)
{
    CSeasonality^ _Cseasonality;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Inflation::ISeasonality^>::IsSome::get (seasonality))
        {
            _Cseasonality = safe_cast<CSeasonality^> (seasonality->Value);
            _Cseasonality->Lock();
        }
        boost::shared_ptr<QuantLib::Seasonality>& _seasonality = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Inflation::ISeasonality^>::IsSome::get (seasonality) ? static_cast<boost::shared_ptr<QuantLib::Seasonality>&> (_Cseasonality->GetShared ()) : boost::shared_ptr<QuantLib::Seasonality>()); //6
    	(*_ppInflationTermStructure)->setSeasonality ( _seasonality );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cseasonality != nullptr) _Cseasonality->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

